package com.lovo.bean;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.web.bind.annotation.PathVariable;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "student_info")
@ApiModel
public class StudentBean {
    @Id
    // 数据库自增长
//    @GeneratedValue(strategy = GenerationType.IDENTITY)
    // 最大ID+1
    @GenericGenerator(name = "hbincrement",strategy = "increment")
    @GeneratedValue(generator = "hbincrement")
    @Column(name = "stu_id")
    @ApiModelProperty(name = "id",value = "学生ID",dataType = "Long",example = "1")
    private Long id;
    @Column(name = "stu_name")
    @NotBlank
    @ApiModelProperty(name = "name",value = "学生姓名",required = true,dataType = "string",example = "张三")
    private String name;
    @Column(name = "gender")
    @Pattern(regexp = "^[男|女]$")
    @ApiModelProperty(name = "gender",value = "性别",notes = "性别必须为男或女",dataType = "string",example = "男")
    private String gender;
    @Max(50)
    @Min(20)
    @Column(name = "age")
    @ApiModelProperty(name = "age",value = "年龄",notes = "年龄必须为20-50之间",dataType = "Integer",example = "30")
    private Integer age;
//    @OneToOne(cascade = CascadeType.PERSIST)
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "detail_id")
    private StudentDetailBean studentDetailBean;
    @ManyToOne
    @JoinColumn(name = "cls_id")
    private ClassBean classBean;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "student_course"
            ,joinColumns = @JoinColumn(name = "stu_id")
            ,inverseJoinColumns = @JoinColumn(name = "course_id"))
    @JsonIgnoreProperties({"studentBeans"})
    private List<CourseBean> courseBeans = new ArrayList<>();

    @Override
    public String toString() {
        return "StudentBean{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}
